{- This file is part of funbot.
 -
 - Written in 2015, 2016, 2017 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

module FunBot.Settings.Sections.Channels
    ( chanSec
    , addChannel
    , addLocalLocation
    , removeLocalLocation
    )
where

import Control.Monad (unless, void)
import Control.Monad.Trans.Class (lift)
import Data.Bool (bool)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Sequence (Seq, (|>), (><), ViewL (..))
import Data.Settings.Section hiding (delete)
import Data.Settings.Types
import Database.Persist
import Network.IRC.Fun.Bot.IrcLog
import Network.IRC.Fun.Bot.MsgCount
import Network.IRC.Fun.Bot.Nicks
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Types.Base (Channel (..), Nickname (..))

import qualified Data.CaseInsensitive as CI
import qualified Data.HashMap.Lazy as M
import qualified Data.HashSet as S
import qualified Data.Sequence as Q
import qualified Data.Text as T

import FunBot.Model
import FunBot.Settings.MkOption
import FunBot.Settings.Persist
import FunBot.Types
import FunBot.Util

defChan = ChanSettings Nothing False "(?)" S.empty Nothing

locationOption chan l@(LocationLabel t) =
    let defl = "(?)"
        getl =
            maybe defl (unLocation . localLocationContent . entityVal) <$>
            getBy (UniqueLocalLocation chan l)
        setl v =
            void $
            upsert
                (LocalLocation chan l $ LocationText v)
                [LocalLocationContent =. LocationText v]
    in  (CI.original t, mkOptionP getl setl defl)

-- | Create a section for a channel.
chanSec :: Channel -> [LocationLabel] -> SettingsTree
chanSec chan lls = Section
    { secOpts = M.fromList
        [ ( "track"
          , mkOptionB
                (channelIsTracked chan)
                (bool (stopTrackingChannel chan) (startTrackingChannel chan))
                False
          )
        , ( "count"
          , mkOptionB
                (chanIsCounted chan)
                (bool (stopCountingChan chan) (startCountingChan chan))
                False
          )
        , ( "log"
          , mkOptionB
                (channelIsLogged chan)
                (bool (stopLoggingChannel chan) (startLoggingChannel chan))
                False
          )
        , ( "def-response"
          , mkOptionB
                (defRespEnabled chan)
                (void . setDefResp chan)
                True
          )
        , ( "say-titles"
          , mkOptionF
                (getf Nothing csSayTitles)
                (setf $ \ cs say -> cs { csSayTitles = say })
                Nothing
          )
        , ( "welcome"
          , mkOptionF
                (getf False csWelcome)
                (setf $ \ cs w -> cs { csWelcome = w })
                False
          )
        , ( "folks"
          , mkOptionP
                (map (unNickname . folkNickname . entityVal) <$>
                    selectList [FolkChannel ==. chan] []
                )
                (\ names -> do
                    olds <- map (folkNickname . entityVal) <$>
                        selectList [FolkChannel ==. chan] []
                    let news = map Nickname names
                        remove = filter (`notElem` news) olds
                        add = filter (`notElem` olds) news
                    deleteWhere [FolkChannel ==. chan, FolkNickname <-. remove]
                    insertMany_ $ map (Folk chan) add
                )
                []
          )
        , ( "email"
          , mkOptionF
                (getf "(?)" csEmail)
                (setf $ \ cs s -> cs { csEmail = s })
                "(?)"
          )
        , ( "browse"
          , mkOptionF
                (getf "" $ fromMaybe "" . csBrowse)
                (setf $ \ cs url ->
                    if T.null url
                        then cs { csBrowse = Nothing }
                        else cs { csBrowse = Just url }
                )
                ""
          )
        ]
    , secSubs = M.fromList
        [ ( "locations"
          , Section
                { secOpts = M.fromList $ map (locationOption chan) lls
                , secSubs = M.empty
                }
          )
        ]
    }
    where
    getf e f = maybe e f . M.lookup chan . stChannels
    setf f v s =
        let chans = stChannels s
            cs = M.lookupDefault defChan chan chans
            cs' = f cs v
            chans' = M.insert chan cs' chans
        in  s { stChannels = chans' }

-- | Add a new channel to state and tree and to be joined from now on. If
-- already exists, nothing happens.
addChannel :: Channel -> BotSession ()
addChannel chan = do
    selectChannel chan
    addChannelState chan
    sets <- getSTree
    let route = ["channels", unChannel chan]
    unless (route `memberSub` sets) $ do
        let sec = chanSec chan []
            ins = insertSub route sec
        modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }

-- | Add a new location item to a channel's settings and tree. Return 'Nothing'
-- on success. Otherwise return whether the channel isn't selected ('False') or
-- the location label already exists ('True').
addLocalLocation
    :: Channel
    -> LocationLabel
    -> LocationText
    -> BotSession (Maybe Bool)
addLocalLocation chan label location = do
    sel <- channelSelected chan
    if sel
        then do
            mlid <- runDB $ insertUnique $ LocalLocation chan label location
            case mlid of
                Nothing -> return $ Just True
                Just _  -> do
                    let (t, opt) = locationOption chan label
                        path = ["channels", unChannel chan, "locations", t]
                        ins = insertOpt path opt
                    modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }
                    return Nothing
        else return $ Just False

-- | Remove a channel-specific location from settings and tree. Return whether
-- success, i.e. whether the location did exist and indeed has been deleted.
removeLocalLocation :: Channel -> LocationLabel -> BotSession Bool
removeLocalLocation chan label = runDB $ do
    mel <- getBy $ UniqueLocalLocation chan label
    case mel of
        Nothing              -> return False
        Just (Entity lid _l) -> do
            delete lid
            let t = CI.original $ unLocationLabel label
                path = ["channels", unChannel chan, "locations", t]
                del = deleteOpt path
            lift $ modifyState $ \ s -> s { bsSTree = del $ bsSTree s }
            return True
